<template>
  <div class="h100 vehicleVersionPage">
    <div class="vehicleVersionSearch table-page-search-wrapper">
      <vehicle-version-search
        :versionType="versionType"
        v-model="queryParams"
        @change="$nextTick(()=>searchData(true))"
        @clickbtn="handleClickSearchBtn"></vehicle-version-search>
    </div>
    <div style="">
      <a-radio-group buttonStyle="solid" v-model="queryParams.fromGcy" style="float:right;margin-bottom: 8px;padding-right:25px;" @change="searchData(true)">
        <a-radio-button :value="false">
          规划院
        </a-radio-button>
        <a-radio-button :value="true">
          工程院
        </a-radio-button>
      </a-radio-group>
    </div>
    <div class="vehicleVersionMain">
      <vehicle-version-table-list
        :versionType="versionType"
        :queryParams="queryParams"
        ref="vehicleVersionMainComp"
        @clickbtn="handleMainCompClick"
      ></vehicle-version-table-list>
    </div>
    <custom-modal
      :width="1100"
      :visible="visible"
      :title="popupsTitle"
      :destroyOnClose="true"
      class="largeVersionPopup"
      @cancel="handleCancel"
      @ok="handleOk"
      modalCls=".largeVersionPopup"
      :bodyStyle="{ height: '500px', overflowY: 'auto' }">
      <edit-vehicle-version :time="time" :versionType="versionType" :id="id" ref="editVehicleVersion"></edit-vehicle-version>
    </custom-modal>
  </div>
</template>
<script>
import VehicleVersionSearch from './components/VehicleVersionSearch.vue'
import VehicleVersionTableList from './components/VehicleVersionTableList.vue'
import { mapState, mapGetters } from 'vuex'
import { minxinDrawerUse } from '@/utils/mixin'
import { softwarePerms } from '@/config/permission/index'
import EditVehicleVersion from './components/EditVehicleVersion'
import VehicleVersionDetail from './components/VehicleVersionDetail'
import VersionTabs from "@views/software/partsSoftware/components/VersionTabs";
import CustomModal from '@/components/CustomModal/index.vue'

export default {
  mixins: [minxinDrawerUse],
  components: {
    VehicleVersionSearch,
    VehicleVersionTableList,
    EditVehicleVersion,
    VehicleVersionDetail,
    CustomModal
  },
  computed: {
    ...mapState({
      commonQueryParams: (state) => state.vehicleVersion.queryParams
    }),
    ...mapGetters(['myActiveProjectId'])
  },
  data () {
    let path = this.$route.path;
    return {
      queryParams: { ...this.commonQueryParams, fromGcy: false, createTimeGte: undefined, createTimeLte: undefined },
      versionType: path.substring(path.lastIndexOf('/') + 1).toUpperCase(),
      nowTime: 156789,
      actions: {
        // 新建车辆版本
        cclbb: {
          method: 'addVehicleVersion'
        }
      },
      visible: false,
      popupsTitle: '',
      time: undefined,
      id: ''
    };
  },
  watch: {
  },
  created() {
    const actionObj = this.actions[this.$route.query.action]
    // 从别的页面跳转过来调用相应的功能
    if (actionObj && actionObj.method) {
      this[actionObj.method]()
    }
    this.getVersionTypeFromPath(this.$route.path);
  },
  mounted() {
    // this.getVersionTypeFromPath(this.$route.path);
  },
  methods: {
    getVersionTypeFromPath(path) {
      let versionType = path.substring(path.lastIndexOf('/') + 1);
      if (versionType !== 'test' && versionType !== 'prod') {
        this.$message.error("找不到页面！");
        this.$router.push("/404/inner");
      }
      this.versionType = versionType.toUpperCase();
    },
    handleClickSearchBtn (data) {
      this.nowTime = new Date().getTime()
      if (this[data.type]) {
        this[data.type](data.data)
      }
    },
    handleMainCompClick (data) {
      this.nowTime = new Date().getTime()
      if (this[data.type]) {
        this[data.type](data.data)
      }
    },
    searchData (isToFirstPage) {
      this.$refs.vehicleVersionMainComp.refreshData(isToFirstPage)
    },
    // 新建车辆版本
    addVehicleVersion () {
      this.visible = true
      this.popupsTitle = this.$t('new') + this.$t('smartDrivingDomainVersion')
      if (this.versionType === 'TEST') {
        this.popupsTitle += ' 【测试版】'
      } else {
        this.popupsTitle += ' 【正式版】'
      }
      this.time = Date.now()
      // const _this = this
      // const buttons = this.getRightDrawerFooterButtons({
      //   key: softwarePerms.vehicleVersion.add,
      //   method: 'saveEditVehicleVersionData',
      //   callback () {
      //     _this.searchData(true)
      //   }
      // })
      // const vars = { time: Date.now(), class: 'rightDrawerWithoutCol', versionType: this.versionType }
      // let title = this.$t('new') + this.$t('vehicleVersion');
      // if (this.versionType === 'TEST') {
      //   title += ' 【测试版】';
      // } else {
      //   title += ' 【整车版】';
      // }
      // this.showRightDrawerNoReset(EditVehicleVersion, title, buttons, 'huge', vars);
    },
    // 编辑车辆版本
    editVehicleVersion (record) {
      this.visible = true
      this.popupsTitle = this.$t('edit') + this.$t('smartDrivingDomainVersion')
      if (this.versionType === 'TEST') {
        this.popupsTitle += ' 【测试版】'
      } else {
        this.popupsTitle += ' 【正式版】'
      }
      this.id = record.id
      // const _this = this
      // const buttons = this.getRightDrawerFooterButtons({
      //   key: softwarePerms.vehicleVersion.edit,
      //   method: 'saveEditVehicleVersionData',
      //   callback () {
      //     _this.searchData(false)
      //   }
      // })
      // const vars = { time: Date.now(), class: 'rightDrawerWithoutCol', id: record.id, versionType: this.versionType }
      // let title = this.$t('edit') + this.$t('vehicleVersion');
      // if (this.versionType === 'TEST') {
      //   title += ' 【测试版】';
      // } else {
      //   title += ' 【整车版】';
      // }
      // this.showRightDrawer(EditVehicleVersion, title, buttons, 'huge', vars)
    },
    // saveEditVehicleVersionData (data) {
    //   const method = data.id ? 'editVehicleVersionData' : 'addVehicleVersionData'
    //   return this.$store.dispatch(method, data)
    // },
    // 查看车辆版本详情
    showVehicleVersionDetail (record) {
      const vars = {
        id: record.id,
        time: Date.now(),
        versionType: this.versionType
      }
      let title = this.$t('smartDrivingDomainVersion') + this.$t('detail')
      if (this.versionType === 'TEST') {
        title += ' 【测试版】';
      } else {
        title += ' 【正式版】';
      }
      this.showRightDrawer(VehicleVersionDetail, title, [], 'large', vars)
    },
    // 关闭弹窗
    handleCancel () {
      this.searchData(true)
      this.visible = false
      this.id = ''
    },
    // 保存
    handleOk () {
      this.$refs.editVehicleVersion.getFormData().then((data) => {
        const method = data.id ? 'editVehicleVersionData' : 'addVehicleVersionData'
        this.$store.dispatch(method, data).then(() => {
          this.searchData(true)
          this.visible = false
          this.id = ''
        })
      })
    }
  },
  beforeDestroy () {
    this.$store.commit('resetVehicleVersionQueryParams')
  }
}
</script>
<style lang="less" scoped>
@import url('./style/list.less');
.largeVersionPopup {}
</style>
